//
//  MemoView.m
//  KidReading
//
//  Created by telen on 15/1/4.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "MemoView.h"
#import "ConstDefine.h"

@interface MemoView()<UITextViewDelegate>
@end

@implementation MemoView
{
    UITextView* textview;
    UIView* navView;
}
@synthesize isInShowing = isTransformWorking;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView* bgv = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:bgv];
        UIImage* img = [UIImage imageNamed:@"memo_bg.png"];
        img = [img stretchableImageWithLeftCapWidth:img.size.width/2 topCapHeight:img.size.height/2];
        bgv.image = img;
        bgview = bgv;
        //
        CGFloat ratio_w = [DataProcessing shareSingle].ratio_w;
        if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            ratio_w /= 1.30;
        }
        CGFloat pod = floor(6/ratio_w);
        frame.size.width -= pod*2;
        frame.origin.x += pod;
        frame.origin.y += pod;
        frame.size.height = ceil(46/ratio_w);
        navView = [[UIView alloc] initWithFrame:frame];
        navView.backgroundColor = [UIColor colorWithHexString:@"#76A1E5"];
        CGFloat radius = floor(10/ratio_w);
        [navView viewAddBezierPathWithRoundedRect:navView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)];
        [self addSubview:navView];
        //
        frame.size.height = 180/ratio_w;
        frame.size.width = 240/ratio_w;
        textview = [[UITextView alloc] initWithFrame:frame];
        textview.center = CGPointMake(self.center.x, self.center.y+23/ratio_w);
        textview.font = [UIFont systemFontOfSize:18/ratio_w];
        textview.delegate = self;
        [self addSubview:textview];
        //
        [self addTapGesture_Undo];
        [self addPanGesture_CanMove];
    }
    return self;
}

- (void)setPt_unshowFollowView:(MemoButton *)pt_unshowFollowView
{
    if (_pt_unshowFollowView) {
        _pt_unshowFollowView.selected = NO;
    }
    _pt_unshowFollowView = pt_unshowFollowView;
    _pt_unshowFollowView.selected = YES;
}

- (void)setTopBtns:(NSArray<UIView *> *)topBtns
{
    CGFloat ratio = [DataProcessing shareSingle].ratio_w;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        ratio /= 1.30;
    }
    if (_topBtns && _topBtns.count > 0) {
        [_topBtns enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
    }
    _topBtns = topBtns;
    //
    CGFloat pod = 6/ratio;
    CGFloat w = navView.bounds.size.width;
    CGFloat cy = navView.bounds.size.height*0.5;
    __block CGFloat podw = 0;
    if (_topBtns && _topBtns.count > 0) {
        [_topBtns enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [navView addSubview:obj];
            CGFloat wobj = obj.bounds.size.width;
            obj.center = CGPointMake(w-pod-podw-wobj*0.5, cy);
            podw += wobj+pod;
        }];
    }
}

- (NSString *)text
{
    return textview.text;
}
- (void)setText:(NSString *)str
{
    textview.text = str;
}

- (void)updateText:(NSString *)str
{
    [self setText:str];
    self.pt_unshowFollowView.txt = str;
    if (self.pt_unshowFollowView.oldTxt != str && ![self.pt_unshowFollowView.oldTxt isEqualToString:str]) {
        [self.readView.currentPageView.memoView list_ModyBtn:self.pt_unshowFollowView];
    }
}

- (void)textBecomeFirstResponder
{
    [textview becomeFirstResponder];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    Boolean textFirst = NO;
    if (self.textToBecomeFirstResponder) {
        __weak MemoView* _self = self;
        textFirst = self.textToBecomeFirstResponder(_self);
    }
    return !textFirst;
}

- (void)dealloc
{
#if __Debug_is_dealloced_log
    NSLog(@"dealloc: %@",NSStringFromClass([self class]));
#endif
}

- (void)show
{
    if (_pt_unshowFollowView) {
        _pt_unshowFollowView.selected = YES;
    }
    if (isTransformWorking) {
        return;
    }
    isTransformWorking = YES;
    if(self.pt_unshowFollowView.superview && self.superview)self.center = [self.superview convertPoint:self.pt_unshowFollowView.center fromView:self.pt_unshowFollowView.superview];
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.hidden = NO;
        self.center = self.pt_show;
    }completion:^(BOOL finished) {
        isTransformWorking = NO;
    }];
}

- (void)unShow
{
    if (isTransformWorking) {
        return;
    }
    [self unShow_must];
}

- (void)unShow_must
{
    isTransformWorking = YES;
    [textview resignFirstResponder];
    if (self.pt_unshowFollowView) {
        self.pt_unshowFollowView.selected = NO;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeScale(0.01, 0.01);
        self.hidden = NO;
        if(self.pt_unshowFollowView.superview && self.superview)self.center = [self.superview convertPoint:self.pt_unshowFollowView.center fromView:self.pt_unshowFollowView.superview];
    }completion:^(BOOL finished) {
        self.hidden = YES;
        isTransformWorking = NO;
    }];
}

@end
